package com.controller;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.bean.wd_ht_build;
import com.bean.wd_ht_project;
import com.bean.wd_ht_room;
import com.bean.wd_opening_report;
import com.bean.wd_pay_type;
import com.service.OpenService;
import com.util.Export;
import com.util.ReadExcel;
@Controller
@RequestMapping("/kaipan")
public class OpenHouseAction {
	/**
	 * String转Date
	 * @param binder
	 */
	@InitBinder  
	public void initBinder(WebDataBinder binder) {  
	SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");  
	dateFormat.setLenient(false);  
	binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true)); 
	}
	
	@Autowired
	 private OpenService  openuservice;
	/**
	 * 查询开盘报告所有信息
	 * @param model
	 * @return
	 * @throws UnsupportedEncodingException 
	 */
	@RequestMapping(value="/openlist.do")
	public String penList(Model model,String reportname) throws UnsupportedEncodingException{
		String rname =reportname;
		 wd_opening_report wbean = new wd_opening_report();
		if(reportname!=null){
			 rname =new String(reportname.getBytes("ISO-8859-1"),"UTF-8");
			 wbean.setOpeningReportName(rname);
		}	
			List<wd_opening_report> list =openuservice.selectAll(wbean);
			model.addAttribute("olist", list);
				return "openlist";
}
	/**
	 * 查询楼项目信息，用于下拉
	 * @param model
	 * @return
	 */
 	@RequestMapping(value="/goadd.do")
	public String addBefore(Model model){
		//添加之前查询所有项目
 		List<wd_ht_project> blist = openuservice.selectObject();
 		model.addAttribute("blist", blist);
		return "addopen";
	}
 	/**
 	 * 添加开盘报告信息
 	 * @param reportbean
 	 * @return 
 	 */
	@RequestMapping(value="/addreport.do")
	public String addReport(wd_opening_report reportbean,HttpSession session){
		String reportid=openuservice.getUuid();
		session.setAttribute("reportid", reportid);
		reportbean.setOpeningReportId(reportid);
		openuservice.insert(reportbean);
		return "redirect:selectRoom.do?projectid="+reportbean.getProjectId();
		
	}
	/**
	 * 根据项目id查询所有房间信息
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/selectRoom.do")
	public String selectRoomByBid(Model model,String projectid,HttpSession session){
		//通过项目id查询所有已开盘房间
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("projectid", projectid);
		map.put("roomtype", 0);
		String reportid=session.getAttribute("reportid")+"";
		map.put("reportid", reportid);
		List<wd_ht_project> roomlist = openuservice.selectRoomByBid(map);
		List<wd_ht_room> pinJie = pinJie(roomlist);
		model.addAttribute("roomlist",pinJie);
		//通过项目id查询项目名称
		wd_ht_project projectbean = openuservice.selectObjectById(projectid);
		model.addAttribute("projectbean", projectbean);
		return "ChooseRoom";
	}
	/**
	 * 获取开盘添加房间信息,一对多再对多
	 * @param model
	 * @param projectid
	 * @return
	 */
	@RequestMapping(value="/alertadd.do")
	public String alertAdd(Model model,String projectid){
		model.addAttribute("projectid", projectid);
		//此方法在下面
		List<wd_ht_room> collectRoom = getCollectRoom(projectid);
		model.addAttribute("leftlist",collectRoom);
		return "AlertRoomAdd";
	}
	/**
	 * 位置切换方法
	 * @param model
	 * @param ids
	 * @return
	 */
	@RequestMapping(value="changes.do")
	public String goChange(Model model,String ids,String projectid,String upid,HttpSession session){
		model.addAttribute("projectid", projectid);
		List<String> list = new ArrayList<String>();
		String arr[] = ids.split(",");
		for(int i =1;i<arr.length;i++){
			list.add(arr[i].trim());
		}
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("idlist", list);
		if(upid!=null){
			String uparr[] = upid.split(",");
			for(int i =1;i<uparr.length;i++){
				list.add(uparr[i].trim());
			}
			Map<String,Object> upmap = new HashMap<String,Object>();
			upmap.put("idlist", list);
			map.put("tid", 0);
			upmap.put("tid", 3);
			openuservice.changeTypeIn(upmap);
		}else{
			map.put("tid", 0);
			openuservice.changeTypeIn(map);
		}
		List<wd_ht_project> prlist =openuservice.selectIn(map);
		//右侧list
		List<wd_ht_room> rightlist = pinJie(prlist);
		//把开盘报告id存入房间
		if(upid==null){
		Map<String,Object> roomap=new HashMap<String, Object>();
		List<String> roist = new ArrayList<String>();
		for (wd_ht_room roombean : rightlist) {
			roist.add(roombean.getRoomId());
		}
		roomap.put("rolist",roist);
		String reportid=session.getAttribute("reportid")+"";
		roomap.put("reportid", reportid);
		openuservice.insertImportid(roomap);
		}
		List<wd_ht_room> leftlist = getCollectRoom(projectid);
		model.addAttribute("rightlist", rightlist);
		model.addAttribute("leftlist", leftlist);
		return "AlertRoomAdd";
	}
	/**
	 * 进入导入价格页面
	 * @param model
	 * @param projectid
	 * @return
	 */
	@RequestMapping(value="/goprice.do")
	public String goPrice(Model model,String projectid,HttpSession session){
		session.setAttribute("projectid", projectid);
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("projectid", projectid);
		map.put("roomtype", 0);
		String reportid=session.getAttribute("reportid")+"";
		map.put("reportid", reportid);
		List<wd_ht_project> manylist = openuservice.selectRoomByBid(map);
		List<wd_ht_room> roomlist = pinJie(manylist);
		model.addAttribute("roomlist", roomlist);
		return "ImportPrice";
	}
	/**
	 * exil导出
	 * @param response
	 * @param jsons
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value="/importexil.do")
	public String importexil(HttpServletResponse response,String jsons) throws IOException{
		String json =new String(jsons.getBytes("ISO-8859-1"),"UTF-8");
		String s1[] = json.split("!");
		List llist = new ArrayList();
		for(int i =0;i<s1.length;i++){
			String s2[] = s1[i].split(",");
			wd_ht_room bjbean = new wd_ht_room();
			for(int x= 0;x<s2.length;x++){
				bjbean.setRoomId(s2[0]);
				bjbean.setDeleteUser(s2[1]);
				bjbean.setRoomCode(s2[2]);
				bjbean.setBuildUpArea(Float.parseFloat(s2[3]));
				bjbean.setInnerArea(Float.parseFloat(s2[4]));
				bjbean.setUpdateUser(s2[5]);
				bjbean.setUnitPrice(Float.parseFloat(s2[6]));
				bjbean.setInnerPrice(Float.parseFloat(s2[7]));
			}
		llist.add(bjbean);
		} 
    response.setHeader("Content-Disposition","attachment;filename=exportdata.xls");//指定下载的文件名  
    response.setContentType("application/vnd.ms-excel");  
    Export  ex = new Export();   
    ex.buildStudentExcel(llist, "exportdata.xls",response.getOutputStream());
		return null;
	}
	/**
	 * 表格导入
	 * @param projectid
	 * @param request
	 * @return
	 */
	@RequestMapping("/impro.do")
	public String updatePrice(String FileName,HttpSession session) throws IOException{
		System.out.println(FileName);
		String pathName = FileName+".xls";
		System.out.println(pathName);
		wd_ht_room roomBean = new wd_ht_room();
		int j = 0;
		ReadExcel ex = new ReadExcel();
		String realPath="C:/Users/Administrator/Desktop/"+pathName;
		List<List<Object>> readExcel = ex.readExcel(new File(realPath));
		for (List<Object> list : readExcel) {
			if(j>0){
			for (int i = 0; i < list.size(); i++) {
				System.out.println(list.get(i));
				roomBean.setRoomId(list.get(0).toString());
				Float unitPrice = Float.parseFloat((list.get(6))+"");
				Float innerPrice = Float.parseFloat(list.get(7).toString());
				roomBean.setUnitPrice(unitPrice);
				roomBean.setInnerPrice(innerPrice);
				//修改价格方法
				 openuservice.updatePrice(roomBean);
			}
		}
			j++;	
	}
		String projectid=session.getAttribute("projectid")+"";
		return "redirect:goprice.do?projectid="+projectid;
		//return null;
}
/**
 * 引入价格折扣
 * @param ztype
 * @param reportid
 * @return
 * @throws UnsupportedEncodingException 
 */
@RequestMapping(value="/addzhekou.do")
public String upZheKou(String zhekou,HttpSession session) throws UnsupportedEncodingException{
	String reportid=session.getAttribute("reportid")+"";
	String zhekoua =new String(zhekou.getBytes("ISO-8859-1"),"UTF-8");
	Map<String,String> map=new HashMap<String, String>();
	map.put("zhekou", zhekoua);
	map.put("reportid", reportid);
	openuservice.upZheKou(map);
	return "redirect:getpaytype.do";
}
/**
 * 查询支付方式
 * @return
 */
@RequestMapping(value="/getpaytype.do")
public String  getpayType(Model model){
	List<wd_pay_type> list=openuservice.getpayType();
	model.addAttribute("paylist", list);
	return "importPayType";
}
/**
 * 把支付方式和开盘id存入第三张表
 * @return
 */
@RequestMapping(value="/addpaytype.do")
public String addPayType(String payid,HttpSession session){
	String reportid=session.getAttribute("reportid")+"";
	Map<String,String> map=new HashMap<String, String>();
	map.put("payid", payid);
	map.put("reportid", reportid);
	openuservice.addPayType(map);
	return "redirect:getfianlAll.do";
}
@RequestMapping(value="/getfianlAll.do")
public String getfianlAll(Model model,HttpSession session){
	//获取开盘报告id
	String reportid=session.getAttribute("reportid")+"";
	//通过报告id获取项目至房间信息
	List<wd_ht_project> projectlist=openuservice.getfianlAll(reportid);
	List<wd_ht_room> alllist = pinJie(projectlist);
	model.addAttribute("alllist", alllist);
	//通过报告id获取支付方式id
	String payid =openuservice.getPayType(reportid);
	//通过报告id获取报告信息
	wd_opening_report reportbean =openuservice.getReportBean(reportid);
	model.addAttribute("reportname", reportbean.getOpeningReportName());
	//通过获取支付方式id获取支付信息
	wd_pay_type paybean=openuservice.getpaybean(payid);
	model.addAttribute("pname", paybean.getPayTypeName());
	return "saveAndSee";
}
/**
 * 查看单个报告信息
 * @param model
 * @param reportid
 * @return
 */
@RequestMapping(value="/seeonereport.do")
public String seeOneReport(Model model,String reportid){
	//通过报告id获取项目至房间信息
		List<wd_ht_project> projectlist=openuservice.getfianlAll(reportid);
		List<wd_ht_room> alllist = pinJie(projectlist);
		model.addAttribute("alllist", alllist);
		//通过报告id获取支付方式id
		String payid =openuservice.getPayType(reportid);
		//通过报告id获取报告信息
		wd_opening_report reportbean =openuservice.getReportBean(reportid);
		model.addAttribute("reportname", reportbean.getOpeningReportName());
		//通过获取支付方式id获取支付信息
		wd_pay_type paybean=openuservice.getpaybean(payid);
		model.addAttribute("pname", paybean.getPayTypeName());
	return "SeeoneReport";
}
	/**
	 * 通过项目id查询楼栋和房间信息并拼接
	 * @param projectid
	 * @return
	 */
	public List<wd_ht_room> getCollectRoom(String projectid){
		Map<String,Object> map = new HashMap<String, Object>();
		map.put("projectid", projectid);
		map.put("roomtype", 3);
		List<wd_ht_project> manylist = openuservice.selectToMany(map);
		List<wd_ht_room> pinJielist = pinJie(manylist);
		return pinJielist;
	}
	/**
	 * 对楼栋和房间进行拼接
	 * @param manylist
	 * @return
	 */
	public List<wd_ht_room> pinJie(List<wd_ht_project> manylist){
		List<wd_ht_room>rlist = new ArrayList<wd_ht_room>();
		for (wd_ht_project projectbean : manylist) {
			List<wd_ht_build> buildlist = projectbean.getBuildlist();
			for (wd_ht_build build : buildlist) {
				List<wd_ht_room> roomlist = build.getRoomlist();
				for (wd_ht_room roombean : roomlist) {
					String buildname=build.getBuildName();
					String roomname=roombean.getRoomCode();
					roombean.setRoomCode(buildname+"-"+roomname);
					//代替项目名称字段
					roombean.setDeleteUser(projectbean.getProjectName());
					rlist.add(roombean);
				}
			}
			
		}
			return rlist;
	}
	
}